<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="项目编号" prop="projectNumber">
      <template>
        <el-select v-model="dataForm.productNumber" :disabled="dataForm.id != 0" placeholder="请选择" @change="getProductInfo()">
          <el-option
              v-for="item in option"
              :key="item.prProjectNumber"
              :label="item.prProjectName"
              :value="item.prProjectNumber">
          </el-option>
        </el-select>
      </template>
    </el-form-item>
    <el-form-item label="检验类别" prop="inspectionType">
          <el-radio class="square-radio" v-model="dataForm.inspectionType" label="1">推广鉴定</el-radio>
          <el-radio class="square-radio" v-model="dataForm.inspectionType" label="2">国家支持的推广鉴定</el-radio>
          <el-radio class="square-radio" v-model="dataForm.inspectionType" label="3">注册确认</el-radio>
          <el-radio class="square-radio" v-model="dataForm.inspectionType" label="4">专项鉴定</el-radio>
          <el-radio class="square-radio" v-model="dataForm.inspectionType" label="5">日常监督</el-radio>
          <el-radio class="square-radio" v-model="dataForm.inspectionType" label="6">专项监督</el-radio>
          <el-radio class="square-radio" v-model="dataForm.inspectionType" label="7">委托检测</el-radio>
<!--      <el-input v-model="dataForm.inspectionType" placeholder="检验类别"></el-input>-->
    </el-form-item>
    <el-form-item label="样品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="样品名称"></el-input>
    </el-form-item>
    <el-form-item label="样品型号" prop="model">
      <el-input v-model="dataForm.model" placeholder="样品编号"></el-input>
    </el-form-item>
    <el-form-item label="起始时间" prop="startingTime">
      <el-date-picker
        v-model="dataForm.startingTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期">
      </el-date-picker>
<!--      <el-input v-model="dataForm.startingTime" placeholder="起始时间"></el-input>-->
    </el-form-item>
    <el-form-item label="结束时间" prop="terminalTime">
      <el-date-picker
          v-model="dataForm.terminalTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
      </el-date-picker>
<!--      <el-input v-model="dataForm.terminalTime" placeholder="结束时间"></el-input>-->
    </el-form-item>
    <el-form-item label="附记" prop="excursus">
      <el-input v-model="dataForm.excursus" placeholder="附记"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        type: [],
        option: [],
        dataForm: {
          id: 0,
          productNumber: '',
          userId: '',
          status: '',
          inspectionType: '',
          productName: '',
          model: '',
          startingTime: '',
          terminalTime: '',
          undeDept: '',
          undeDeptOpinion: '',
          undeDeptTime: '',
          projectUndertaker: '',
          projectUndertakerOpinion: '',
          projectUndertakerTime: '',
          identificationDivision: '',
          identificationDivisionOpinion: '',
          identificationDivisionTime: '',
          projectLeader: '',
          responsibleOpinion: '',
          responsibleTime: '',
          auditor: '',
          auditorOpinion: '',
          auditorTime: '',
          approver: '',
          approverOpinion: '',
          approverTime: '',
          excursus: ''
        },
        dataRule: {
          productId: [
            { required: true, message: '项目id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          inspectionType: [
            { required: true, message: '检验类别不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '样品名称不能为空', trigger: 'blur' }
          ],
          model: [
            { required: true, message: '样品编号不能为空', trigger: 'blur' }
          ],
          startingTime: [
            { required: true, message: '起始时间不能为空', trigger: 'blur' }
          ],
          terminalTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          undeDept: [
            { required: true, message: '项目承担部门不能为空', trigger: 'blur' }
          ],
          undeDeptOpinion: [
            { required: true, message: '项目承担部门意见不能为空', trigger: 'blur' }
          ],
          undeDeptTime: [
            { required: true, message: '项目承担部门时间不能为空', trigger: 'blur' }
          ],
          projectUndertaker: [
            { required: true, message: '项目承担人不能为空', trigger: 'blur' }
          ],
          projectUndertakerOpinion: [
            { required: true, message: '项目承担人意见不能为空', trigger: 'blur' }
          ],
          projectUndertakerTime: [
            { required: true, message: '项目承担人时间不能为空', trigger: 'blur' }
          ],
          identificationDivision: [
            { required: true, message: '鉴定科不能为空', trigger: 'blur' }
          ],
          identificationDivisionOpinion: [
            { required: true, message: '鉴定科意见不能为空', trigger: 'blur' }
          ],
          identificationDivisionTime: [
            { required: true, message: '鉴定科时间不能为空', trigger: 'blur' }
          ],
          projectLeader: [
            { required: true, message: '项目负责人不能为空', trigger: 'blur' }
          ],
          responsibleOpinion: [
            { required: true, message: '负责人意见不能为空', trigger: 'blur' }
          ],
          responsibleTime: [
            { required: true, message: '负责人时间不能为空', trigger: 'blur' }
          ],
          auditor: [
            { required: true, message: '审核人不能为空', trigger: 'blur' }
          ],
          auditorOpinion: [
            { required: true, message: '审核人意见不能为空', trigger: 'blur' }
          ],
          auditorTime: [
            { required: true, message: '审核人时间不能为空', trigger: 'blur' }
          ],
          approver: [
            { required: true, message: '批准人不能为空', trigger: 'blur' }
          ],
          approverOpinion: [
            { required: true, message: '批准人意见不能为空', trigger: 'blur' }
          ],
          approverTime: [
            { required: true, message: '批准人时间不能为空', trigger: 'blur' }
          ],
          excursus: [
            { required: true, message: '附记不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
      this.getProductNumber()
    },
    methods: {
      getProductNumber(){
        this.$http({
          url: this.$http.adornUrl('/reportapproval/product/getProjectNumber'),
          method: 'get',
          data: this.$http.adornParams({})
        }).then((data) => {
          if (data && data.code === 0) {
            this.option = data.productList
            // console.log(this.options)
          }else {
            this.$message.error(data.msg)
          }
        })
      },
      getProductInfo(){
        this.$http({
          url: this.$http.adornUrl(`/reportapproval/product/getProductInfo/${this.dataForm.productNumber}`),
          method: 'get',
          data: this.$http.adornParams({})
        }).then((data) => {
          if (data && data.code === 0) {
            this.dataForm.productName = data.productInfo.prProjectName
            this.dataForm.model = data.productInfo.prProducerModel
            // console.log(this.dataForm.productName)
          }else {
            this.$message.error(data.msg)
          }
        })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/reportapproval/inspresupro/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.productNumber = data.inspResuPro.productNumber
                this.dataForm.userId = data.inspResuPro.userId
                this.dataForm.status = data.inspResuPro.status
                this.dataForm.inspectionType = data.inspResuPro.inspectionType
                this.dataForm.productName = data.inspResuPro.productName
                this.dataForm.model = data.inspResuPro.model
                this.dataForm.startingTime = data.inspResuPro.startingTime
                this.dataForm.terminalTime = data.inspResuPro.terminalTime
                this.dataForm.undeDept = data.inspResuPro.undeDept
                this.dataForm.undeDeptOpinion = data.inspResuPro.undeDeptOpinion
                this.dataForm.undeDeptTime = data.inspResuPro.undeDeptTime
                this.dataForm.projectUndertaker = data.inspResuPro.projectUndertaker
                this.dataForm.projectUndertakerOpinion = data.inspResuPro.projectUndertakerOpinion
                this.dataForm.projectUndertakerTime = data.inspResuPro.projectUndertakerTime
                this.dataForm.identificationDivision = data.inspResuPro.identificationDivision
                this.dataForm.identificationDivisionOpinion = data.inspResuPro.identificationDivisionOpinion
                this.dataForm.identificationDivisionTime = data.inspResuPro.identificationDivisionTime
                this.dataForm.projectLeader = data.inspResuPro.projectLeader
                this.dataForm.responsibleOpinion = data.inspResuPro.responsibleOpinion
                this.dataForm.responsibleTime = data.inspResuPro.responsibleTime
                this.dataForm.auditor = data.inspResuPro.auditor
                this.dataForm.auditorOpinion = data.inspResuPro.auditorOpinion
                this.dataForm.auditorTime = data.inspResuPro.auditorTime
                this.dataForm.approver = data.inspResuPro.approver
                this.dataForm.approverOpinion = data.inspResuPro.approverOpinion
                this.dataForm.approverTime = data.inspResuPro.approverTime
                this.dataForm.excursus = data.inspResuPro.excursus
              }
            })
          }
        })
      },
      closeDialog() {
        this.resetForm();
        this.$emit('refreshDataList');
      },
      resetForm() {
        // this.$refs.form.resetFields();
        this.dataForm = {};
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/reportapproval/inspresupro/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'productNumber': this.dataForm.productNumber,
                'userId': this.dataForm.userId,
                'status': this.dataForm.status,
                'inspectionType': this.dataForm.inspectionType,
                'productName': this.dataForm.productName,
                'model': this.dataForm.model,
                'startingTime': this.dataForm.startingTime,
                'terminalTime': this.dataForm.terminalTime,
                'undeDept': this.dataForm.undeDept,
                'undeDeptOpinion': this.dataForm.undeDeptOpinion,
                'undeDeptTime': this.dataForm.undeDeptTime,
                'projectUndertaker': this.dataForm.projectUndertaker,
                'projectUndertakerOpinion': this.dataForm.projectUndertakerOpinion,
                'projectUndertakerTime': this.dataForm.projectUndertakerTime,
                'identificationDivision': this.dataForm.identificationDivision,
                'identificationDivisionOpinion': this.dataForm.identificationDivisionOpinion,
                'identificationDivisionTime': this.dataForm.identificationDivisionTime,
                'projectLeader': this.dataForm.projectLeader,
                'responsibleOpinion': this.dataForm.responsibleOpinion,
                'responsibleTime': this.dataForm.responsibleTime,
                'auditor': this.dataForm.auditor,
                'auditorOpinion': this.dataForm.auditorOpinion,
                'auditorTime': this.dataForm.auditorTime,
                'approver': this.dataForm.approver,
                'approverOpinion': this.dataForm.approverOpinion,
                'approverTime': this.dataForm.approverTime,
                'excursus': this.dataForm.excursus
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
